<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('collectionManagement.collectionLog.createTime')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.reminder')">
          <el-date-picker
            v-model="remindTime"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.logType')">
          <el-select v-model="searchForm.logTypeList" clearable multiple collapse-tags :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('collectionManagement.collectionLog.typeData')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.company')">
          <el-select v-model="searchForm.firmName" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in allFirm"
              :key="item.id"
              :label="item['firmName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.personnel')">
          <el-input
            v-model="searchForm.operationName"
            clearable
            :maxlength="50"
            :placeholder="$t('common.input')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.phone')">
          <el-input
            v-model="searchForm.phoneNumber"
            clearable
            :maxlength="50"
            :placeholder="$t('common.input')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.collectionLog.orderNo')">
          <el-input
            v-model="searchForm.orderNo"
            clearable
            :maxlength="50"
            :placeholder="$t('common.input')"
          />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="orderNo"
          :label="tableText['orderNo']"
          min-width="170"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="toDetail(scope.row)">
              {{ scope.row.orderNo }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="operationName"
          :label="tableText['personnel']"
          min-width="170"
        />
        <el-table-column
          prop="firmName"
          :label="tableText['company']"
          min-width="170"
        />
        <el-table-column
          prop="phonenumer"
          :label="tableText['phone']"
          min-width="150"
        />
        <el-table-column
          prop="createTime"
          :label="tableText['createTime']"
          min-width="170"
        />
        <el-table-column
          prop="logType"
          :label="tableText['logType']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['logType'],'collectionManagement.collectionLog.typeData') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          :label="tableText['title']"
          min-width="200"
        />
        <el-table-column
          prop="remindTime"
          :label="tableText['reservation']"
          min-width="170"
        />
        <el-table-column
          prop="remindContent"
          :label="tableText['content']"
          min-width="200"
        />
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { listUrgentRecallLog, getAllOverdueFirm, exportListUrgentRecallLog } from '@/api/collection'
import { formatDate, ObjToQueryString } from '@/utils/method'
export default {
  name: 'CollectionLog',
  data() {
    return {
      loading: true,
      chType: 1,
      time: [new Date(), new Date()],
      remindTime: null,
      allFirm: [],
      tableData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('collectionManagement.collectionLog')
    this.getAllFirm()
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        orderNo: null,
        phoneNumber: null,
        createTimeStart: null,
        createTimeEnd: null,
        remindTimeStart: null,
        remindTimeEnd: null,
        operationName: null,
        firmName: '',
        logTypeList: [1, 2, 3, 4, 5, 6],
        exportExcel: false,
        columns: ['log.create_time'],
        orderBy: [true]
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.time = [new Date(), new Date()]
      this.remindTime = null
      this.searchForm = this.params()
      this.getData()
    },
    changeStatus(type) {
      this.chType = type
      this.getData()
    },
    searchData() {
      const data = {
        orderNo: this.searchForm.orderNo,
        phoneNumber: this.searchForm.phoneNumber,
        operationName: this.searchForm.operationName,
        firmName: this.searchForm.firmName,
        logTypeList: this.searchForm.logTypeList,
        exportExcel: this.searchForm.exportExcel,
        pageSize: this.searchForm.pageSize,
        currentPage: this.searchForm.currentPage,
        columns: this.searchForm.columns,
        orderBy: this.searchForm.orderBy
      }
      if (this.time && this.time.length > 1) {
        data['createTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['createTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['createTimeStart'] = null
        data['createTimeEnd'] = null
      }
      if (this.remindTime && this.remindTime.length > 1) {
        data['remindTimeStart'] = formatDate(new Date(this.remindTime[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['remindTimeEnd'] = formatDate(new Date(this.remindTime[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['remindTimeStart'] = null
        data['remindTimeEnd'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      listUrgentRecallLog(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    exportList() {
      const data = this.searchData()
      data['exportExcel'] = true
      exportListUrgentRecallLog(data)
    },
    toDetail(item) {
      const paramData = {
        from: 'caseManagement',
        orderNo: item['orderNo'],
        userId: item['userId'],
        creditOrderId: item['creditOrderId'],
        orderStatus: item['orderStatus'],
        orderId: item['orderId'],
        staffID: item['staffID'],
        staffName: item['staffName'],
        recallPhone: this.recallPhone,
        recallPhonePassword: this.recallPhonePassword
      }
      const para = ObjToQueryString(paramData)
      window.open(`${window.location.origin + window.location.pathname}#/collectionCenter/collectDetail?${para}`, '_blank')
    },
    setValue(val, data) {
      const name = this.$t(`${data}`).filter(item => {
        return item.value === val
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getAllFirm() {
      getAllOverdueFirm()
        .then(res => {
          if (res.success) {
            this.allFirm = res.data
            const obj = [{
              firmName: this.$t('common.all'),
              id: ''
            }]
            this.allFirm = obj.concat(this.allFirm)
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    test(data) {
      let value = this.searchForm[data]
      if (value.length > 0) {
        value = value.replace(/[^0-9]/g, '')
      }
      this.searchForm[data] = value
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
